let http = {};
import axios from 'axios';
import utils from '@/utils/utils';
import store from '@/store/index';
import config from '@/config/config';

// 自定义返回处理
function custResponseFun(res, resolve) {
    // 返回的状态
    const responseStatus = res.data.success;
    // const responseMsg = res.data.msg || res.data.messageList;
    switch (responseStatus) {
        case true:
            resolve(res);
            break;
        case false:
            resolve(res);
            utils.error(res.data.msg || '服务器异常，请检查网络或联系管理员')
            break;
    }
}

// 自定义异常处理方式
function custErrFun(err) {
    console.log('err',err.response.status)
    if(err.status == 500 || (err.response && err.response.status == 500)){
        utils.error(err.response.data.msg || '请求失败，请检查网络或联系管理员');
    } else if (err.response && err.response.status !== 200) {
        if(err.response.status == 403){
            utils.error(err.response.data.msg || '登录状态过期，请重新登录');
            window.location.href = '/'
        } else {
            utils.error(err.response.data.msg || '请求失败，请检查网络或联系管理员');
        }
    } else if (err.message && err.stack) {
        console.log(err);
    } else {
        utils.error('请求失败，请检查网络或联系管理员');
    }
}

// get 请求
http.get = function (url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params || {},
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
                'x-access-token': store.state.user.token
            },
            timeout: config.httpTimeOut
        })
            .then(res => {
                custResponseFun(res, resolve);
            })
            .catch(err => {
                resolve({data:{status:false},status:false})
                custErrFun(err)
            })
    });
};

// POST 请求
http.post = function (url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params, {
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
                'x-access-token': store.state.user.token
            },
            timeout: config.httpTimeOut
        })
            .then(res => {
                custResponseFun(res, resolve);
            })
            .catch(err => {
                resolve({data:{status:false},status:false})
                custErrFun(err)
            })
    });
};

// DELETE 请求
http.del = function (url) {
    return new Promise((resolve, reject) => {
        axios.delete(url, {
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
                'x-access-token': store.state.user.token
            },
            timeout: config.httpTimeOut
        })
            .then(res => {
                custResponseFun(res, resolve);
            })
            .catch(err => {
                resolve({data:{status:false},status:false})
                custErrFun(err)
            })
    });
};

http.postFormData = function (url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params, {
            headers: {
                'Content-Type': 'multipart/form-data',
                'x-access-token': store.state.user.token
            },
            timeout: config.httpTimeOut
        })
            .then(res => {
                custResponseFun(res, resolve);
            })
            .catch(err => {
                resolve({data:{status:false},status:false})
                utils.error(err.response.data.msg || '服务器错误')
            })
    });
};

// PUT 请求
http.put = function (url, params) {
    return new Promise((resolve, reject) => {

        axios.put(url, params, {
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
                'x-access-token': store.state.user.token
            },
            timeout: config.httpTimeOut
        })
            .then(res => {
                custResponseFun(res, resolve);
            })
            .catch(err => {
                resolve({data:{status:false},status:false})
                utils.error(err.response.data.msg || '服务器错误')
            })
    });
};

export default http;
